package org.sam.swing.table.matrixImpl;

import java.util.List;
import java.util.Map;

import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableBuilder;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableColumnModel;
import org.sam.swing.table.JSTableModel;
import org.sam.swing.table.JSTableModelLinster;

/**
 * 默认的tablemodel和columnmodel实现
 * 
 * @author sam
 *
 * @param <E>
 */
public class JSTableMatrixBuilderImpl<G, T> implements JSTableBuilder<Map<G, List<T>>> {

	/**
	 * 当前的column列表
	 */
	private JSTableColumn[] columns;

	/**
	 * 当前操作的column列表
	 * 
	 * @return
	 */
	public JSTableColumn[] getColumns() {
		return columns;
	}

	/**
	 * 当前操作的column列表
	 * 
	 * @param columns
	 */
	public void setColumns(JSTableColumn[] columns) {
		this.columns = columns;
	}

	/**
	 * 缓存的colmodel对象
	 */
	private JSTableColumnModel colModel;

	/**
	 * 缓存的tablemodel对象
	 */
	private JSTableModel<Map<G, List<T>>> tableModel;

	/**
	 * 带有列信息的构造函数
	 * 
	 * @param cols
	 */
	public JSTableMatrixBuilderImpl(JSTableColumn... cols) {
		this.columns = cols;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSTableColumnModel buildTableColumnModel() throws Exception {
		JSTableColumnModel colModel = new JSTableMatrixColumnModel();

		if (this.columns == null || this.columns.length <= 0)
			throw new Exception("no columns");

		int i = 0;
		for (JSTableColumn col : this.columns) {
			if (col.getModelIndex() < 0) {
				col.setModelIndex(i);
			}
			colModel.addColumn(col);
			i++;
		}
		this.colModel = colModel;
		this.getTableModel().addTableModelListener((JSTableMatrixColumnModel) this.colModel);

		return colModel;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSTableModel<Map<G, List<T>>> buildTableModel() throws Exception {
		JSTableMatrixModel<G, T> tabModel = new JSTableMatrixModel<>();

		if (this.columns == null || this.columns.length <= 0)
			throw new Exception("no columns");

		for (JSTableColumn col : this.columns) {
			tabModel.addColumn(col);
		}

		// 生成原始数据的列
		if (tabModel.findColumn(JSTableColumn.COLUMN_ORIGINAL) < 0) {
			JSTableColumn column = new JSTableColumn();
			column.setTitle(JSTableColumn.COLUMN_ORIGINAL);
			column.setHeaderValue(JSTableColumn.COLUMN_ORIGINAL);
			column.setVisible(false);
			column.setEditable(false);
			tabModel.addColumn(column);
		}
		this.tableModel = tabModel;

		return tabModel;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSTableModelLinster<Map<G, List<T>>> buildModelLinster(JSTableModel<Map<G, List<T>>> tableModel)
			throws Exception {
		JSTableModelLinster<Map<G, List<T>>> adapter = new JSTableModelMatrixAdapter<>();
		tableModel.setTableModelLinster(adapter);
		return adapter;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSTableModel<Map<G, List<T>>> getTableModel() throws Exception {
		if (this.tableModel == null)
			this.tableModel = buildTableModel();

		return this.tableModel;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSTableColumnModel getTableColumnModel() throws Exception {
		if (this.colModel == null)
			this.colModel = buildTableColumnModel();

		return this.colModel;
	}
	

	/**
	 * 用之前2步创建好的对象，直接生成表格
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public JSTable buildTable() throws Exception {
		JSTableModel<Map<G, List<T>>> tableModel = this.getTableModel();
		JSTableColumnModel tableColumnModel = this.getTableColumnModel();
		JSTable table = this.buildTable(tableModel, tableColumnModel);
		JSTableModelMatrixAdapter<G,T> adapter = (JSTableModelMatrixAdapter<G,T>)this.buildModelLinster(tableModel);
		adapter.setColModel((JSTableMatrixColumnModel)tableColumnModel);
		adapter.setTable(table);
		
		table.initConfig();
		table.setSortable(false);
		return table;
	}
}